// utils/permission.js
export async function ensurePermissions(scopes) {
  for (const scope of scopes) {
    const res = await new Promise(resolve => {
      wx.getSetting({
        success(setting) {
          if (setting.authSetting[scope]) return resolve(true);
          wx.authorize({
            scope,
            success: () => resolve(true),
            fail: () => resolve(false)
          });
        }
      });
    });

    if (!res) {
      const readable = {
        'scope.camera': '摄像头',
        'scope.writePhotosAlbum': '保存图片到相册',
        'scope.album': '相册读取' // ⚠️ 实际上“相册读取”不需要授权，这里是为了说明用
      }[scope] || scope;

      wx.showModal({
        title: '权限请求失败',
        content: `请在设置中手动开启 ${readable} 权限`,
        success(res) {
          if (res.confirm) wx.openSetting();
        }
      });
    }
  }
}
